/**
 * Created by wangzi on 2017/3/9.
 */

$(function () {

    //table2_demo4_js_start
    //表格(DataTables)-4，综合示例
    var language = {
        search: '搜索：',
        lengthMenu: "每页显示 _MENU_ 记录",
        zeroRecords: "没找到相应的数据！",
        info: "分页 _PAGE_ / _PAGES_",
        infoEmpty: "暂无数据！",
        infoFiltered: "(从 _MAX_ 条数据中搜索)",
        paginate: {
            first: '首页',
            last: '尾页',
            previous: '上一页',
            next: '下一页',
        }
    }
    $("#table2_demo4").dataTable({
        autoWidth: false,
        lengthChange: true, //不允许用户改变表格每页显示的记录数
        pageLength: 40, //每页显示几条数据
        lengthMenu: [10, 20, 40], //每页显示选项
        pagingType: 'full_numbers',
        // ajax : './data.json',
        ordering: true,
        // columns : [
        //   {data:"id",orderable: false},
        //   {data:"ip"},
        //   {data:"user"},
        //   {data:"mark"},
        //   {data:"pid"},
        //   {data:"script"},
        //   {
        //     data:null,
        //     orderable:false,
        //     render : function(data, type, row, meta){
        //         return '<a class="king-btn king-default del">操作</a>';
        //     }
        //   }
        // ],
        language: language
    });

    var t = $("#table2_demo4").DataTable();//获取datatables对象
    //删除按钮绑定事件
    $("#table2_demo4 tbody").on('click', 'button.king-btn', function () {
        var row = t.row($(this).parents('tr')),//获取按钮所在的行
            data = row.data();


        if (data[4] == '运行中') {

            if (confirm('确定要停止' + data[0] + '@' + data[2] + '脚本 ?')) {
                var mark = data[2];
                var ip = data[0];
                window.console.log(app_id);
                $.ajax({
                    type: 'get',
                    // url : site_url +'/process/stopprocess/?ip='+ip+'&mark='+mark,
                    url: '/process/stopprocess/?ip=' + ip + '&mark=' + mark,
                    success: function (rdata) {
                        window.console.log(rdata);

                        window.location.reload();
                        var d = dialog({
                            width: 260,
                            // content: '<img class="mr5" src="' + site_url + '/static/img/battery_36.gif">停止进程中'
                                                  content: '<img class="mr5" src="/static/img/battery_36.gif">停止进程中'

                        });
                                                $("#mask").show();

                        d.show();
                        setTimeout(function () {
                            d.close().remove();
                                                         $("#mask").hide();

                        }, 5000);
                    }

                });

            }
        }

        if (data[4] == '未运行') {

            if (confirm('确定要启动' + data[0] + '@' + data[2] + '脚本 ?')) {
                var mark = data[2];
                var ip = data[0];
                $.ajax({
                    type: 'get',
                    // url : site_url +'/process/startprocess/?ip='+ip+'&mark='+mark,
                    url: '/process/startprocess/?ip=' + ip + '&mark=' + mark,

                    success: function (rdata) {
                        window.console.log(rdata);
                        window.location.reload();

                        window.location.reload();

                        var d = dialog({
                            width: 260,
                            // content: '<img class="mr5" src="'+site_url+'/static/img/battery_36.gif">启动进程中'
                            content: '<img class="mr5" src="/static/img/battery_36.gif">启动进程中'

                        });
                        $("#mask").show();
                        d.show();
                        setTimeout(function () {
                            d.close().remove();
                             $("#mask").hide();
                        }, 5000);


                    }

                });


            }
        }
    });
});
